<?php
class ProductController extends FrontendController
{
	public function actionIndex()
	{
		$this->render('index');
	}
	public function actionDetail()
	{
		$baseUrl = Yii::app()->baseUrl;
		$cs = Yii::app()->getClientScript();
		$cs->registerScriptFile($baseUrl.'/themes/default/js/jquery-ui.js');
		$id = Yii::app()->request->getParam('id','');
		$data = FrontendShopProductsModel::getDetail($id);
		$this->render('detail', array(
					'data'=>$data
				));
	}
	public function actionCategory()
	{
		$catId = Yii::app()->request->getParam('id','');
		$this->render('category', array(
				'catID'=>$catId
		));
	}
	public function actionSearch()
	{
		$category = isset($_GET['category'])?$_GET['category']:6;
		$manufactor = isset($_GET['manufactor'])?$_GET['manufactor']:'';
		$price = isset($_GET['price'])?$_GET['price']:'';
		$keyword = isset($_GET['keyword'])?$_GET['keyword']:'';
		
		$order = "lastest";
		$orderQuery="";
		if(isset($_POST['Products'])){
			$order = $_POST['Products']['order'];
			switch ($order){
				case 'hight':
					$orderQuery = "ORDER BY price DESC";
					break;
				case 'low':
					$orderQuery = "ORDER BY price ASC";
					break;
				default:
					$orderQuery = "";
					break;
			}
		
		}
		
		$total = FrontendShopProductsModel::getTotalProductBySearch($category, $keyword, $price);
		$page = new CPagination($total);
		$itemOnPaging = 5;
		$page->pageSize = Yii::app()->params['perpage'];
		$curr_page = $page->getCurrentPage();
		
		
		$data = FrontendShopProductsModel::getProductBySearch($category, $keyword, $price, $page->getLimit(), $page->getOffset(), $orderQuery);
		
		$this->render('search', array(
					'catID'=>$category,
					'total'=>$total,
					'data'=>$data,
					'page'=>$page,
					'curr_page'=>$curr_page,
					'order'=>$order
				));
	}
}